const {
  baseUrl
} = require("../../../../config/api");

// pages/usercenter/gongdan/exampleList/index.js
Page({
  data: {
    viewer: {
      visible: false,
      showIndex: false,
      closeBtn: false,
      images: [],
    },
    list: [],
    confirmBtn: {
      content: '知道了',
      variant: 'base'
    },
    showText: false,
    showMultiText: false,
    showTextAndTitle: false,
    showMultiTextAndTitle: false,
    showDialog: false
  },
  showDialog(e) {
    this.setData({
      showDialog: true,
    });
  },
  closeDialog() {
    this.setData({
      showDialog: false,
    });
  },
  preview(e) {
    console.log(e)
    this.setData({
      viewer: {
        images: [
          e.currentTarget.dataset.item
        ],
        showIndex: true,
        visible: true,
        closeBtn: true
      }
    });
  },
  onClose() {
    this.setData({
      viewer: {
        visible: false
      },
    });
  },
  //下载模板图片
  DownLoad(event) {
    var path = event.currentTarget.dataset.path;
    let sp = path.split('.')
    if (sp === undefined) return
    let suffix = sp[sp.length - 1]

    if (path === undefined || path === '' || suffix === undefined || suffix === '') return
    wx.showLoading({
      title: '下载中...',
    })
    wx.downloadFile({
      url: path, //仅为示例，并非真实的资源
      success(res) {
        wx.hideLoading()
        if (res.statusCode === 200) {
          wx.openDocument({
            filePath: res.tempFilePath,
            fileType: suffix,
            showMenu: true,
            success(res) {
              console.log(res)
            },
            fail(res) {
              console.log(res)
            }
          })
        }
      },
      fail: function (res) {
        console.log('下载文件失败', res)
      }
    })
  },
  //获取模板基础数据
  onShow() {
    let _this = this
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: baseUrl + 'sys/worksheetTemplate/list',
      method: 'GET',
      success: (res) => {
        wx.hideLoading()
        if (res.data.code === 0) {
          _this.setData({
            list: res.data.data
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'error',
            duration: 2000
          });
        }
      }

    })
  },
})